<?php
require_once dirname(__FILE__).'/Google/Client.php';
require_once dirname(__FILE__).'/Google/Service/YouTube.php';
require_once ("/virtual/twotiger/lib/library/SecureManager.class.php");
$secureManager = new SecureManager();



// development
$OAUTH2_CLIENT_ID = '30540061275-unl9s4srbctm2vuja284qliaip1rpc85.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = '_cFXYfWBR0e57Zn2VYHyi1rW';


$playlist_title='Test Playlist  ' . date("Y-m-d H:i:s");
if(isset($_GET['playlist_title'])){
  $playlist_title = $secureManager->sanitize($_GET['playlist_title']);
}

$video_id='SZj6rAYkYOg';
if(isset($_GET['video_id'])){
  $video_id = $secureManager->sanitize($_GET['video_id']);
}

$redirect = $redirect = "http://".$_SERVER["HTTP_HOST"]."/Index/oauth";


$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setAccessType('offline'); // default: offline
$client->setApplicationName('moreplaylist');
$client->setDeveloperKey("AIzaSyCZSoT7Ouj8FU0O7yWK4jLdBRx8crNFiuw"); // API key
$client->setRedirectUri($redirect);

$youtube = new Google_Service_YouTube($client);


if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.' .  strval($_SESSION['state']) . ":" .strval($_GET['state']) );
  }
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  try {
    $playlistSnippet = new Google_Service_YouTube_PlaylistSnippet();
    $playlistSnippet->setTitle($playlist_title);
    $playlistSnippet->setDescription('A private playlist created with MorePlaylist');
    $playlistStatus = new Google_Service_YouTube_PlaylistStatus();
    $playlistStatus->setPrivacyStatus('private');
    $youTubePlaylist = new Google_Service_YouTube_Playlist();
    $youTubePlaylist->setSnippet($playlistSnippet);
    $youTubePlaylist->setStatus($playlistStatus);
    $playlistResponse = $youtube->playlists->insert('snippet,status', $youTubePlaylist, array());
    $playlistId = $playlistResponse['id'];

    // 5. Add a video to the playlist. First, define the resource being added
    // to the playlist by setting its video ID and kind.
    $resourceId = new Google_Service_YouTube_ResourceId();
    $resourceId->setVideoId($video_id);
    $resourceId->setKind('youtube#video');

    $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
    $playlistItemSnippet->setTitle('First video in the test playlist');
    $playlistItemSnippet->setPlaylistId($playlistId);
    $playlistItemSnippet->setResourceId($resourceId);

    $playlistItem = new Google_Service_YouTube_PlaylistItem();
    $playlistItem->setSnippet($playlistItemSnippet);
    $playlistItemResponse = $youtube->playlistItems->insert(
        'snippet,contentDetails', $playlistItem, array());

    $htmlBody .= "<h3>New Playlist</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
        $playlistResponse['snippet']['title'],
        $playlistResponse['id']);
    $htmlBody .= '</ul>';

    $htmlBody .= "<h3>New PlaylistItem</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
        $playlistItemResponse['snippet']['title'],
        $playlistItemResponse['id']);
    $htmlBody .= '</ul>';

  } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  }

  $_SESSION['token'] = $client->getAccessToken();
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>New Playlist</title>
</head>
<body>
  <?=$htmlBody?>
    
<p><a href="/Index">back</a> </p>
</body>
</html>


?>
